﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblChipImageDAO
    {
         private DBService _db = null;

         public TblChipImageDAO(string ConnectionString)
         {
            _db = new DBService(ConnectionString);
         }

         // lay hinh anh tuong ung voi id
         public TblChipImage GetChipImageByID(int IDChip, int ID)
         {

             TblChipImage image = new TblChipImage();
             try
             {
                 DataTable dt = _db.ExeSP_DataTable("sp_TblChipImage_Lst",
                    new string[] { "@p_IDChip", "@Id" },
                    new object[] { IDChip, ID },
                   CommandType.StoredProcedure);

                 foreach (DataRow dr in dt.Rows)
                 {
                     image.ID = Convert.ToInt32(dr["ID"].ToString());
                     image.ImageURL = dr["ImageURL"].ToString();
                     image.IsDefault = Convert.ToBoolean(dr["IsDefault"].ToString());
                     image.IDChip = Convert.ToInt32(dr["IDChip"].ToString());

                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return image;
         }

         public bool InsImageChip(TblChipImage imgChip, int mode)
         {
             bool flag = false;
             try
             {
                 _db.ExeSP_NonQuery("sp_TblChipImage_Ins",
                     new string[] { "@p_IDChip", "@p_ImageURL" },
                     new object[] { imgChip.IDChip, imgChip.ImageURL },
                     CommandType.StoredProcedure);
                 flag = true;
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return flag;
         }

         public bool DeleteImgChip(TblChipImage imgChip)
         {
             bool flag = false;
             try
             {
                 _db.ExeSP_NonQuery("delete TblChipImage where ID =@id",
                     new string[] { "@id" },
                     new object[] { imgChip.ID },
                     CommandType.Text);
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return flag;
         }

         public DataTable GetImageChip(int IDChip)
         {
             DataTable dt = null;
             try
             {
                 dt = _db.ExeSP_DataTable("select * from TblChipImage where IDChip = @IDChip",
                     new string[] { "@IDChip" },
                     new object[] { IDChip },
                     CommandType.Text);
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return dt;
         }
    }
}
